import { defineStore } from 'pinia';
import { nextTick, ref, watch } from 'vue';
import { useRouter } from 'vue-router';

export const useMenuStore = defineStore('menu', {
    state: () => {
        return {
            menuItems: [
            ],
            router: useRouter(),
            isCollapse: ref(false)
        };
    },
    persist: {
        enabled: true,
        strategies: [
            {
                key: 'menuItems',
                storage: localStorage,
            }
        ]
    }
    , actions: {
        async removeMenuItem(val) {
            const router = useRouter();
            for (let i = 0; i < this.menuItems.length; i++) {
                if (val === this.menuItems[i].path && this.menuItems.length > 1) {
                    try {
                        this.menuItems.splice(i, 1);
                        this.router.push({ path: this.menuItems[i - 1] ? this.menuItems[i - 1].path : this.menuItems[i].path });
                    } catch (error) {
                        
                    }
                }
            }
        }
    },
});
